package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.ScoreData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IScoreList;
import com.wbao.dianniu.listener.IScoreListListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * 用户信息修改
 *
 * @author yejianjun
 */
public class ScoreListManager implements IScoreList {
    private IScoreListListener mListener;
    private Context mContext;

    public ScoreListManager(Context context) {
        mContext = context;
    }

    @Override
    public void scoreList(int accountId, int page, int rows) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACCOUNT_SCORE_LIST)
                    .addArgs("accountId", accountId)
                    .addArgs("page", page)
                    .addArgs("rows", rows);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                List<ScoreData> list = JsonUtil.toObjectList(obj, ScoreData.class);
                if (null != mListener)
                    mListener.onScoreListSuccess(list);
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onScoreListFailure(errorCode, msg);
            }
        });
    }

    @Override
    public boolean addScoreListListener(IScoreListListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeScoreListListener(IScoreListListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }
}
